package com.sqly.doc.user;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.ServicePackage;
import com.sqly.doc.pojo.Team;
import com.sqly.doc.util.BinderUtil;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cz.msebera.android.httpclient.Header;

/**
 * 服务包新增或编辑界面
 * Created by Administrator on 2017/11/18 0018.
 */

public class servicepack_add extends MyBaseActivity{
    //标题
    TextView title, uname;
    EditText tdname, total_price, dicount_price;
    ServicePackage obj;
    Context con = servicepack_add.this;
    String url = HttpUrl.SaveServicepack;

    @Override
    protected void initUI() {
        setContentView(R.layout.servicepack_add);
        init();
    }

    private void init(){
        //标题
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        total_price = (EditText) findViewById(R.id.total_price);
        dicount_price = (EditText) findViewById(R.id.dicount_price);


        title.setText("服务包管理");
        uname.setText("保存");
        tdname = (EditText) findViewById(R.id.tdname);

        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != tdname && StringUtils.isEmpty(tdname.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入服务包名称");
                }else if(tdname.getText().toString().length()>21){
                    MyToastUtil.ShowToast(servicepack_add.this, "服务包名称最多输入20个字符！");
                }else if(null != total_price && StringUtils.isEmpty(total_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入总价");
                }else if(!BinderUtil.isNumeric(total_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "总价必须为有效数字");
                }else if(null != dicount_price && StringUtils.isEmpty(dicount_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入折扣价");
                }else if(!BinderUtil.isNumeric(dicount_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "折扣价必须为有效数字");
                }else{
                    if (obj == null) {
                        obj = new ServicePackage();
                    }
                    obj.setName(tdname.getText().toString());
                    obj.setTotal_price(total_price.getText().toString());
                    obj.setDiscount_price(dicount_price.getText().toString());

                    if (NetWorkUtils.isNetworkConnected(con)) {///网络连接
                        RequestParams ps = new RequestParams();
                        ps.add("entity.name", obj.getName());
                        //BigDecimal bd=new BigDecimal(StrBd);
                        //设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
                        //bd=bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                        ps.add("entity.discount_price", obj.getDiscount_price());
                        ps.add("entity.sbmac", obj.getTotal_price());

                        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                        ps.add("type","2");

                        UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                String content = new String(response);
                                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                                if (ret.getCode().equals("200")) {
                                    MyToastUtil.ShowToast(con, "成功");
                                }else{
                                    MyToastUtil.ShowToast(con, ret.getInfo());
                                }
                                finish();
                            }
                            @Override
                            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                                MyToastUtil.ShowToast(con, "数据传输失败");
                            }
                        });
                    } else {//网络没有链接
                        MyToastUtil.ShowToast(con, "请联网操作！");
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }
}
